var TagsEDJX = {
    
    init: function() {
	try {
	    TagsEDJX.listTags();
	} catch(e) {
	    setTimeOut('TagsEDJX.init()', 750);
	}
    },

    tagAdd: function() {
	var input_box = document.getElementById('tagadd');
	var input_button = document.getElementById('taggadd_button');
	input_button.disabled = true;
	input_box.disabled = true;
	var tag_value = input_box.value;
	if(TagsEDJX.tagValidate()) {
	    TagsEDJX.taggaddInvalid_event(false);
	    TagsEDJX.showWaiter();

	    frame = document.createElement('iframe');
	    frame.setAttribute('src', '/edjx/tag/add/' + tool_name + '/' + tag_value);
	    frame.setAttribute('name', 'ajax_callback_x_frame');
	    frame.setAttribute('style', 'display: none;');
	    document.getElementsByTagName('body')[0].appendChild(frame);
	    function ol_tagAdd() {
		setTimeout('TagsEDJX.listTags()', (1000));
		input_box.disabled = false;
		input_button.disabled = true;
		input_box.value = '';
	    }
	    frame.onload = ol_tagAdd();
	} else {
	    TagsEDJX.taggaddInvalid_event(true);
	    input_button.disabled = false;
	    input_box.disabled = false;
	}
    },

    tagValidate: function() {
	var regex = new RegExp(/^[a-zA-Z0-9_]+$/);
	var valid = regex.exec(document.getElementById('tagadd').value);
	if(valid == null) {
	    return false;
	} else {
	    return true;
	}
    },

    showWaiter: function() {
	document.getElementById('taglist_wait').style.visibility = 'visible';
	document.getElementById('taglist').style.visibility = 'hidden';
	document.getElementById('taglist_btn').style.visibility = 'hidden';
	document.getElementById('taglist_wait').style.display = 'inline';
	document.getElementById('taglist').style.display = 'none';
	document.getElementById('taglist_btn').style.display = 'none';
    },

    showSelect: function() {
	document.getElementById('taglist_wait').style.visibility = 'hidden';
	document.getElementById('taglist').style.visibility = 'visible';
	document.getElementById('taglist_btn').style.visibility = 'visible';
	document.getElementById('taglist_wait').style.display = 'none';
	document.getElementById('taglist').style.display = 'inline';
	document.getElementById('taglist_btn').style.display = 'inline';
    },

    listTags: function() {
	frame = document.createElement('iframe');
	frame.setAttribute('src', '/edjx/tag/list/' + tool_name);
	frame.setAttribute('name', 'ajax_callback_x_frame');
	frame.setAttribute('style', 'display: none;');
	document.getElementsByTagName('body')[0].appendChild(frame);
    },

    updateTags: function(ids, names) {
	var selObj = document.getElementById('taglist');
	selObj.options.length = 0;
	for(x in ids) {
	    selObj.options[selObj.options.length] = new Option(names[x], ids[x]);
	}
	TagsEDJX.showSelect();
    },

    tagaddChange_event: function() {
	var input_box = document.getElementById('tagadd');
	var input_button = document.getElementById('taggadd_button');
	if(input_box.value != '') {
	    input_button.disabled = false;
	} else {
	    input_button.disabled = true;
	}
    },

    taggaddInvalid_event: function(boolinValid) {
	if(boolinValid == true) {
	    document.getElementById('tagadd_invalid_alert').style.display = 'block';
	    document.getElementById('tagadd_invalid_alert').style.visibility = 'visible';
	} else {
	    document.getElementById('tagadd_invalid_alert').style.display = 'none';
	    document.getElementById('tagadd_invalid_alert').style.visibility = 'hidden';
	}
    },

    tagSelected_event: function() {
	var selObj = document.getElementById('taglist');
	var selIndx = selObj.selectedIndex;
	if(selIndx == -1) {
	    document.getElementById('taglist_btn').disabled = true;
	} else {
	    document.getElementById('taglist_btn').disabled = false;
	}
    },

    tagDelete_event: function() {
	document.getElementById('taglist_btn').disabled = true;
	var selObj = document.getElementById('taglist');
	var selIndx = selObj.selectedIndex;
	try {
	    TagsEDJX.showWaiter();
	    var tagV = selObj.options[selIndx].value;
	    var tagN = selObj.options[selIndx].text;
	    frame = document.createElement('iframe');
	    frame.setAttribute('src', '/edjx/tag/del/' + tool_name + '/' + tagV);
	    frame.setAttribute('name', 'ajax_callback_x_frame');
	    frame.setAttribute('style', 'display: none;');
	    document.getElementsByTagName('body')[0].appendChild(frame);
	    TagsEDJX.TagExists(false);
	    function ol_tagAdd() {
		setTimeout('TagsEDJX.listTags()', (1000));
	    }
	    frame.onload = ol_tagAdd();
	} catch(e) {
	    TagsEDJX.listTags();
	}
    },
    
    TagExists: function(boolinValid) {
	if(boolinValid == true) {
	    document.getElementById('tagadd_invalid_exists').style.display = 'block';
	    document.getElementById('tagadd_invalid_exists').style.visibility = 'visible';
	} else {
	    document.getElementById('tagadd_invalid_exists').style.display = 'none';
	    document.getElementById('tagadd_invalid_exists').style.visibility = 'hidden';
	}
    }
};

setTimeout('TagsEDJX.init()', (1500));